package com.david.controller;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Mono;

import java.util.Date;

@RestController
public class MainController{
    @RequestMapping(value = "/see",produces="text/event-stream;charset=utf-8")
    public Object xxoo(){
        System.out.println("来了老弟"+Thread.currentThread().getName());
        Date date=new Date();
        return "data:"+date.getTime()+" \n\n";
    }

    //模拟同步
    @GetMapping("/1")
    public String get() throws InterruptedException {
        System.out.println("----1");

        //是service
        String result=getResult();

        System.out.println("----2");

        return result;
    }

    //模拟异步
    @GetMapping("/2")
    public Mono<String> get2(){
        System.out.println("---1");
        Mono<String> result = Mono.create(sink -> {
            try {
                getResult();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });
        System.out.println("---2");
        return result;

    }
    

    private String getResult() throws InterruptedException {
        Thread.sleep(1000);
        return "xxoo";

    }
}
